//
//  YDComposeBottomView.swift
//  YDWeibo01
//
//  Created by suibian on 16/8/17.
//  Copyright © 2016年 iitcast. All rights reserved.
//

import UIKit
//底部视图按钮的枚举
enum YDComposeBottomViewType:Int{
    
    case Toolbar = 0
    case Mention = 1
    case Trend = 2
    case Emoticon = 3
    case Add = 4
    

}
class YDComposeBottomView: UIView {
//   把button取到外面供表情键盘和系统键盘切换使用
    var emoticonButton:UIButton?
//    声明一个闭包
    var closure:((type:YDComposeBottomViewType)->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
//    定义一个属性 供外界调用
    var isEmotion:Bool = false{
    
        didSet{
            let imageName:String
            if isEmotion{
//            显示表情键盘
                imageName = "compose_keyboardbutton_background"
            
            }else{
//            显示系统键盘
                imageName = "compose_emoticonbutton_background"
            
            }
//            设置图片
            emoticonButton?.setImage(UIImage(named: "\(imageName)"), forState: UIControlState.Normal)
            emoticonButton?.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        }
   
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    button 点击事件
    func buttonClick(button:UIButton){
//    执行闭包
        closure?(type: YDComposeBottomViewType(rawValue: button.tag)!)
    }

    private func setUpUI(){
    backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)
//        添加控件
        let toolbarButton = addChildButtons("compose_toolbar_picture",type: .Toolbar )
        let mentionButton = addChildButtons("compose_mentionbutton_background",type: .Mention)
        let trendButton = addChildButtons("compose_trendbutton_background",type: .Trend)
        emoticonButton = addChildButtons("compose_emoticonbutton_background",type: .Emoticon)
        let addButton = addChildButtons("compose_add_background",type: .Add)
//    添加约束
        toolbarButton.snp_makeConstraints { (make) -> Void in
            make.leading.bottom.top.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        mentionButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(toolbarButton.snp_trailing)
            make.bottom.top.equalTo(self)
            make.width.equalTo(trendButton)
        }
        trendButton.snp_makeConstraints { (make) -> Void in
            make.bottom.top.equalTo(self)
            make.leading.equalTo(mentionButton.snp_trailing)
            make.width.equalTo(emoticonButton!)
        }
        emoticonButton!.snp_makeConstraints { (make) -> Void in
            make.bottom.top.equalTo(self)
            make.leading.equalTo(trendButton.snp_trailing)
            make.width.equalTo(addButton)
        }
        addButton.snp_makeConstraints { (make) -> Void in
            make.bottom.top.trailing.equalTo(self)
            make.leading.equalTo(emoticonButton!.snp_trailing)
            
        }
        
    }
    
    func addChildButtons(imgName:String,type:YDComposeBottomViewType) ->UIButton{
    let button = UIButton()
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
//        通过枚举名得到对应的枚举值
        button.tag = type.rawValue
       
        addSubview(button)
    
    
    return button
    }
}
